package org.i1510.common.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.i1510.common.bean.PageBean;
import org.i1510.common.bean.TreeNodeChecked;
import org.i1510.common.dao.ISysPopDao;
import org.i1510.common.dao.ISysRoleDao;
import org.i1510.common.dao.ISysRolePopDao;
import org.i1510.common.po.SysPop;
import org.i1510.common.po.SysRole;
import org.i1510.common.po.SysRolePop;
import org.i1510.common.service.ISysRoleService;
import org.i1510.common.utils.BeanUtils;
import org.i1510.common.utils.DateUtils;
import org.i1510.common.utils.IDGenerator;
import org.i1510.common.utils.UserUtils;
import org.i1510.common.utils.web.ResultMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("iSysRoleService")
public class SysRoleServiceImpl extends BaseServiceImpl implements
		ISysRoleService {
	@Autowired
	private ISysRoleDao iSysRoleDao;
	@Autowired
	private ISysRolePopDao iSysRolePopDao;
	@Autowired
	private ISysPopDao iSysPopDao;

	public List getAll(SysRole sysRole,Map conditions) {
		Map map = new HashMap();
		// Bean转成Map
		BeanUtils.ObjectCopyMap(sysRole, map);
		map.putAll(conditions);
		List list = this.queryForListBySql("sysRoleSql.getAll", map);
		return list;
	}
	/**
	*@Author Rambo
	*@Desc：获取角色权限
	*@param sysRole
	*@param conditions
	*@return TODO
	*@Version  SysRoleServiceImpl.java,v 1.1 2016年7月20日 下午12:02:28 
	*/
	public List findRolePop(SysRole sysRole, Map conditions) {

        Map map = new HashMap();
        //Bean转成Map
        BeanUtils.ObjectCopyMap(sysRole, map);
        map.putAll(conditions);
        //角色所属权限
        List<SysRolePop> rolePopList = iSysRolePopDao.findByRoleId(sysRole.getDocId());
        //权限列表
		List<SysPop> popList = iSysPopDao.findAll();
		List treeNodeList = this.getTreeNode("0000",popList,rolePopList);
        return treeNodeList;
	}
	
		/**
		*@Author Rambo
		*@Desc：功能树
		*@param fdPopId
		*@param popList
		*@param rolePopList
		*@return TODO
		*@Version  SysRoleServiceImpl.java,v 1.1 2016年7月20日 下午1:57:59 
		*/
		private List getTreeNode(String fdPopId,List<SysPop> popList,List<SysRolePop> rolePopList){
			List resultList = new ArrayList();
			//当前权限集合
			List<SysPop> list = this.getChildrens(popList, fdPopId);
			for (SysPop pop : list) {
				TreeNodeChecked treeNodeChecked = new TreeNodeChecked();
				treeNodeChecked.setText(pop.getDocSubject());
				treeNodeChecked.setId(pop.getDocId());
				treeNodeChecked.setIconCls("");
				for (int i = 0; i < rolePopList.size(); i++) {
					SysRolePop rolePop = rolePopList.get(i);
					if(rolePop.getFdPopId().equals(pop.getDocId())){
						treeNodeChecked.setChecked(true);
						break;
					}else{
						treeNodeChecked.setChecked(false);
					}
				}
				treeNodeChecked.setChildren(getTreeNode(pop.getDocId(),popList,rolePopList));//递归
				resultList.add(treeNodeChecked);
			}
			return resultList;
		}
	
		/**
		*@Author Rambo
		*@Desc：子集合
		*@param popList
		*@param fdPopId
		*@return TODO
		*@Version  SysRoleServiceImpl.java,v 1.1 2016年7月20日 下午1:58:09 
		*/
		private List<SysPop> getChildrens(List<SysPop> popList, String fdPopId) {
			List<SysPop> resultList = new ArrayList<SysPop>();
			for (SysPop pop : popList) {
				if(pop.getParentId().equals(fdPopId)) {//父节点id
					resultList.add(pop);
				}
			}
			return resultList;
		}
	@Transactional
	public ResultMapper saveAll(List<SysRole> list) {

		ResultMapper mapper = ResultMapper.generateFailure();

		List<SysRole> listSaved = iSysRoleDao.save(list);
		
		if (listSaved == null) {
			return null;
		}
		mapper = ResultMapper.generateSuccess();
		mapper.appendResult("baseRtn", listSaved);
		return mapper;
	}

	public ResultMapper save(SysRole basePo,HttpServletRequest request) {

		ResultMapper mapper = ResultMapper.generateFailure();

        SysRole saved = null;

        if(null !=basePo && basePo.getDocId() != null  && !basePo.getDocId().equals("")){//修改

            SysRole finder = iSysRoleDao.findById(basePo.getDocId());   

            if(null != finder){

                finder.setDocLastTime(DateUtils.getNow());

                finder.setDocLastor(UserUtils.getSessionUserBean(request).getSysUser().getDocId());            

                BeanUtils.copy(finder, basePo);

                saved = iSysRoleDao.save(finder); 

            }else{

                mapper = ResultMapper.generateFailure();

                return mapper;

            }

            
        }else{//新增

            basePo.setDocId(IDGenerator.getGenerateID());

            basePo.setDocCreateTime(DateUtils.getNow());

            basePo.setDocCreator(UserUtils.getSessionUserBean(request).getSysUser().getDocId());

            saved = iSysRoleDao.save(basePo);

        }

        if (saved == null) {

            mapper = ResultMapper.generateFailure();

            return mapper;

        }else{

            mapper = ResultMapper.generateSuccess();

            mapper.appendResult("docId", saved.getDocId());

            return mapper;            

        }
	}
	public ResultMapper delete(String fdId) {

		ResultMapper mapper = ResultMapper.generateFailure();
		iSysRoleDao.delete(fdId);
		mapper = ResultMapper.generateSuccess();
		return mapper;
	}
	
	public SysRole findById(String fdId){
		SysRole  SysRole = iSysRoleDao.findById(fdId);
		return SysRole;
	}
	public List<SysRole> findByIds(List<String> fdIds){
		return (List<SysRole>) iSysRoleDao.findAll(fdIds);
	}
	public List getPage(SysRole sysRole, PageBean pageBean,Map conditions) {
		Map map = new HashMap();
		// Bean转成Map
		BeanUtils.ObjectCopyMap(sysRole, map);
		map.put("intPageIndex", pageBean.getIntPageIndex());
		map.put("intPageSize", pageBean.getIntPageSize());
		map.putAll(conditions);
		List list = this.queryForListBySql("sysRoleSql.getPage", map);
		return list;
	}

	public int getPageCount(SysRole sysRole,Map conditions) {
		Map map = new HashMap();
		// Bean转成Map
		BeanUtils.ObjectCopyMap(sysRole, map);
		map.putAll(conditions);
		List list = this.queryForListBySql("sysRoleSql.getPageCount", map);
		return list.size();
	}

}